﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Threading;
using System.IO.Ports;
using SerialPortHelperLib;
using XChipTool.Enties;
using System.Configuration;
using BinAnalysisHelp;
using XChip_Common;

namespace XChipTool
{
    public partial class XChip_Gpio : DevExpress.XtraEditors.XtraUserControl
    {
        public SerialPort sPortTemp;
        private bool isWrite = false;
        private addressregisterManager aBll;
        //private List<addressregister> XChipGpio;
        //记录是否使用USB设备读写
        public bool IsUsbOpen = false;
        //记录是否使用I2C设备读写
        public bool m_open = false;
        public XChip_Gpio()
        {
            InitializeComponent();
        }
        public XChip_Gpio(SerialPort _sPortTemp, addressregisterManager _aBll)
        {
            sPortTemp = _sPortTemp;
            aBll = _aBll;
            //XChipGpio = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "awb" || c.Classify == "Common"));
            InitializeComponent();
        }
        private void simpleButton6_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(threadPro);
            thread.Start();
        }

        private void threadPro()
        {
            MethodInvoker methodInvoker = new MethodInvoker(ShowFormB);
            BeginInvoke(methodInvoker);
        }

        private void ShowFormB()
        {
//            string str = @" 倒车线、镜像、翻转配置区可通过选择GPIO脚及其电平属性，控制倒车线状态、镜像与翻转状态；该功能大多应用于车载应用上。
//IRcut配置区可配置IRcut控制模式；
//IRcut自动模式下，当Cur_Gain≥IR全通_GainLimit且Cur_Avg＜IR全通_AvgLimit时切换成黑白模式；当Cur_Gain≤IR截止_GainLimit且Cur_Avg＞IR截止_AvgLimit且Cur_IRgain>IR截止_IRGain时切换成彩色模式；output_pin脚选择用于输出电平的控制引脚选择；
//TDN输入极性控制调整外部输入电平对彩色黑白模式切换极性：正向为高电平时切换成彩色模式；IRC输出极性控制调整IRC_ON/IRC_OFF的电平极性：正向为切换成彩色模式时IRC_ON输出一段时间高电平；
//PWM配置区可通过选择GPIO脚及调整GainLimit控制PWM脉冲波形的输出；
//按键ADC值配置区可配置菜单按键的ADC阻值范围；
//Setting轮询配置区可配置功能使能；system_tick_value调整定时器计数，用于消抖灵敏度调整；";
            string str = "";
            XChip_OptimizationTips ot = new XChip_OptimizationTips("GPIO调优Tips", str);
            ot.StartPosition = FormStartPosition.CenterScreen;
            //ot.ShowDialog();
            ot.Show();
        }
        /// <summary>
        /// 勾选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton5_Click(object sender, EventArgs e)
        {
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            try
            {
                isWrite = true;
                #region Pin脚选择

                long nMask = SerialSend.ReadRegister(0x2000152b, sPortTemp);
                if ((nMask & 0xff) == 0x0)
                {
                    CarBackLine_Pin_Sel.Text = "无";
                }
                else if ((nMask & 0xff) == 0x1)
                {
                    CarBackLine_Pin_Sel.Text = "GPIO0";
                }
                else if ((nMask & 0xff) == 0x2)
                {
                    CarBackLine_Pin_Sel.Text = "ICR_OFF";
                }
                else if ((nMask & 0xff) == 0x3)
                {
                    CarBackLine_Pin_Sel.Text = "ICR_ON";
                }
                else if ((nMask & 0xff) == 0x4)
                {
                    CarBackLine_Pin_Sel.Text = "TDN";
                }
                nMask = SerialSend.ReadRegister(0x2000152c, sPortTemp);
                if ((nMask & 0xff) == 0x0)
                {
                    Mirror_Pin_Sel.Text = "无";
                }
                else if ((nMask & 0xff) == 0x1)
                {
                    Mirror_Pin_Sel.Text = "GPIO0";
                }
                else if ((nMask & 0xff) == 0x2)
                {
                    Mirror_Pin_Sel.Text = "ICR_OFF";
                }
                else if ((nMask & 0xff) == 0x3)
                {
                    Mirror_Pin_Sel.Text = "ICR_ON";
                }
                else if ((nMask & 0xff) == 0x4)
                {
                    Mirror_Pin_Sel.Text = "TDN";
                }
                nMask = SerialSend.ReadRegister(0x2000152d, sPortTemp);
                if ((nMask & 0xff) == 0x0)
                {
                    Flip_Pin_Sel.Text = "无";
                }
                else if ((nMask & 0xff) == 0x1)
                {
                    Flip_Pin_Sel.Text = "GPIO0";
                }
                else if ((nMask & 0xff) == 0x2)
                {
                    Flip_Pin_Sel.Text = "ICR_OFF";
                }
                else if ((nMask & 0xff) == 0x3)
                {
                    Flip_Pin_Sel.Text = "ICR_ON";
                }
                else if ((nMask & 0xff) == 0x4)
                {
                    Flip_Pin_Sel.Text = "TDN";
                }
                //nMask = SerialSend.ReadRegister(0x80140b1c, sPortTemp);
                var nHigh = SerialSend.ReadRegister(0x200015a7, sPortTemp);
                var m1 = SerialSend.ReadRegister(0x200015a6, sPortTemp);
                var m0 = SerialSend.ReadRegister(0x200015a5, sPortTemp);
                var nLow = SerialSend.ReadRegister(0x200015a4, sPortTemp);
                if ((nLow & 0xff) == 0x01)
                {
                    Pwm_Pin_Sel.Text = "GPIO0";
                }
                else if ((nLow & 0xff) == 0x2)
                {
                    Pwm_Pin_Sel.Text = "GPIO1";
                }
                else if ((m0 & 0xff) == 0x40)
                {
                    Pwm_Pin_Sel.Text = "GPIO14";
                }
                else if ((m0 & 0xff) == 0x80)
                {
                    Pwm_Pin_Sel.Text = "GPIO15";
                }
                else if ((m1 & 0xff) == 0x40)
                {
                    Pwm_Pin_Sel.Text = "GPIO22";
                }
                else if ((m1 & 0xff) == 0x80)
                {
                    Pwm_Pin_Sel.Text = "GPIO23";
                }
                else
                {
                    Pwm_Pin_Sel.Text = "无";
                }

                nMask = SerialSend.ReadRegister(0x2000124f, sPortTemp);
                if ((nMask & 0xff) == 0x0)
                {
                    OutPut_pin_Com.Text = "无";
                }
                else if ((nMask & 0xff) == 0x1)
                {
                    OutPut_pin_Com.Text = "Setting_Sel";
                }
                else if ((nMask & 0xff) == 0x2)
                {
                    OutPut_pin_Com.Text = "GPIO0";
                }
                else if ((nMask & 0xff) == 0x3)
                {
                    OutPut_pin_Com.Text = "TDN";
                }
                #endregion

                #region 电平控制
                nMask = SerialSend.ReadRegister(0x2000152a, sPortTemp);
                if ((nMask & 0xff) == 0x0)
                {
                    CarBackLine_Level_Toggle.Text = "高电平有";
                }
                else if ((nMask & 0xff) == 0x1)
                {
                    CarBackLine_Level_Toggle.Text = "低电平有";
                }
                nMask = SerialSend.ReadRegister(0x2000152e, sPortTemp);
                if ((nMask & 0xff) == 0x0)
                {
                    Mirror_Level_Toggle.Text = "高电平镜像";
                }
                else if ((nMask & 0xff) == 0x1)
                {
                    Mirror_Level_Toggle.Text = "低电平镜像";
                }
                nMask = SerialSend.ReadRegister(0x2000152f, sPortTemp);
                if ((nMask & 0xff) == 0x0)
                {
                    Flip_Level_Toggle.Text = "高电平翻转";
                }
                else if ((nMask & 0xff) == 0x1)
                {
                    Flip_Level_Toggle.Text = "低电平翻转";
                }

                nMask = SerialSend.ReadRegister(0x20001529, sPortTemp);
                if ((nMask & 0xff) == 0x0)
                {
                    CarBackLine_Mode.Text = "每帧执行一次";
                }
                else if ((nMask & 0xff) == 0x1)
                {
                    CarBackLine_Mode.Text = "上电执行一次";
                }

                nMask = SerialSend.ReadRegister(0x2000122b, sPortTemp);
                if ((nMask & 0xff) == 0x0)
                {
                    TDN_In_C.Text = "正向";
                }
                else if ((nMask & 0xff) == 0x1)
                {
                    TDN_In_C.Text = "反向";
                }
                nMask = SerialSend.ReadRegister(0x20001233, sPortTemp);
                if ((nMask & 0xff) == 0x1)
                {
                    IRC_In_C.Text = "正向";
                }
                else if ((nMask & 0xff) == 0x0)
                {
                    IRC_In_C.Text = "反向";
                }
                nMask = SerialSend.ReadRegister(0x20001221, sPortTemp);
                if ((nMask & 0xff) == 0x20 || (nMask & 0xff) == 0x21)
                {
                    IR_C_Model.Text = "外部模式";
                    IR_All_GainLimit.Enabled = false;
                    IR_End_GainLimit.Enabled = false;
                    IR_All_AvgLimit.Enabled = false;
                    IR_End_AvgLimit.Enabled = false;
                    IR_End_AwbGainLimit.Enabled = false;
                }
                else if ((nMask & 0xff) == 0x40 || (nMask & 0xff) == 0x41)
                {
                    IR_C_Model.Text = "自动模式";
                    IR_All_GainLimit.Enabled = true;
                    IR_End_GainLimit.Enabled = true;
                    IR_All_AvgLimit.Enabled = true;
                    IR_End_AvgLimit.Enabled = true;
                    IR_End_AwbGainLimit.Enabled = true;
                }

                nMask = SerialSend.ReadRegister(0x20001249, sPortTemp);
                if ((nMask & 0xff) == 0x01)
                {
                    Tve_Wave_sw.Text = "打开";
                }
                else if ((nMask & 0xff) == 0x0)
                {
                    Tve_Wave_sw.Text = "关闭";
                }
                #endregion

                #region 勾选框
                nMask = SerialSend.ReadRegister(0x80500253, sPortTemp);
                if (((nMask & 0x80) >> 7) == 0x1)
                {
                    adc_db_en.Checked = true;
                }
                else
                {
                    adc_db_en.Checked = false;
                }
                nMask = SerialSend.ReadRegister(0x20001248, sPortTemp);
                if ((nMask & 0xff) == 0x1)
                {
                    IRC_en.Checked = true;
                }
                else
                {
                    IRC_en.Checked = false;
                }
                nMask = SerialSend.ReadRegister(0x20001588, sPortTemp);
                if ((nMask & 0xff) == 0x1)
                {
                    PWN_Ctr_en.Checked = true;
                }
                else
                {
                    PWN_Ctr_en.Checked = false;
                }
                nMask = SerialSend.ReadRegister(0x80140b1e, sPortTemp);
                if ((nMask & 0xff) == 0x1)
                {
                    Setting_Polling_ENable.Checked = true;
                }
                else
                {
                    Setting_Polling_ENable.Checked = false;
                }
                nMask = SerialSend.ReadRegister(0x801407fc, sPortTemp);
                if ((nMask & 0xff) == 0x1)
                {
                    System_Tick_Enable.Checked = true;
                }
                else
                {
                    System_Tick_Enable.Checked = false;
                }
                #endregion

                nHigh = SerialSend.ReadRegister(0x2000122d, sPortTemp);
                nLow = SerialSend.ReadRegister(0x2000122c, sPortTemp);
                var limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                IR_All_GainLimit.Text = limit.ToString("X2");

                nHigh = SerialSend.ReadRegister(0x2000122f, sPortTemp);
                nLow = SerialSend.ReadRegister(0x2000122e, sPortTemp);
                limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                IR_End_GainLimit.Text = limit.ToString("X2");

                nHigh = SerialSend.ReadRegister(0x20001483, sPortTemp);
                nLow = SerialSend.ReadRegister(0x20001482, sPortTemp);
                limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                Pulse_Out_GainLimit.Text = limit.ToString("X2");

                nHigh = SerialSend.ReadRegister(0x20001485, sPortTemp);
                nLow = SerialSend.ReadRegister(0x20001484, sPortTemp);
                limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                Pulse_In_GainLimit.Text = limit.ToString("X2");

                nHigh = SerialSend.ReadRegister(0x20001189, sPortTemp);
                nLow = SerialSend.ReadRegister(0x20001188, sPortTemp);
                limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                adc_up_txt.Text = limit.ToString("X2");

                nHigh = SerialSend.ReadRegister(0x2000118b, sPortTemp);
                nLow = SerialSend.ReadRegister(0x2000118a, sPortTemp);
                limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                adc_down_txt.Text = limit.ToString("X2");

                nHigh = SerialSend.ReadRegister(0x2000118d, sPortTemp);
                nLow = SerialSend.ReadRegister(0x2000118c, sPortTemp);
                limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                adc_right_txt.Text = limit.ToString("X2");

                nHigh = SerialSend.ReadRegister(0x2000118f, sPortTemp);
                nLow = SerialSend.ReadRegister(0x2000118e, sPortTemp);
                limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                adc_left_txt.Text = limit.ToString("X2");

                nHigh = SerialSend.ReadRegister(0x20001191, sPortTemp);
                nLow = SerialSend.ReadRegister(0x20001190, sPortTemp);
                limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                adc_menu_txt.Text = limit.ToString("X2");

                nHigh = SerialSend.ReadRegister(0x20001235, sPortTemp);
                nLow = SerialSend.ReadRegister(0x20001234, sPortTemp);
                limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                IR_All_AvgLimit.Text = limit.ToString("X2");

                nHigh = SerialSend.ReadRegister(0x20001237, sPortTemp);
                nLow = SerialSend.ReadRegister(0x20001236, sPortTemp);
                limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                IR_End_AvgLimit.Text = limit.ToString("X2");

                nHigh = SerialSend.ReadRegister(0x2000123b, sPortTemp);
                m1 = SerialSend.ReadRegister(0x2000123a, sPortTemp);
                m0 = SerialSend.ReadRegister(0x20001239, sPortTemp);
                nLow = SerialSend.ReadRegister(0x20001238, sPortTemp);

                IR_End_AwbGainLimit.Text = nHigh.ToString("X2") + m1.ToString("X2") + m0.ToString("X2") + nLow.ToString("X2");
                //adc_chg_masklab
                nHigh = SerialSend.ReadRegister(0x80500257, sPortTemp);
                nLow = SerialSend.ReadRegister(0x80500256, sPortTemp);
                limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                adc_chg_mask.Text = limit.ToString("X2");
                #region 拨码
                nHigh = SerialSend.ReadRegister(0x2000141d, sPortTemp);
                nLow = SerialSend.ReadRegister(0x2000141c, sPortTemp);
                limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                statetxt0.Text = limit.ToString("X2");

                nHigh = SerialSend.ReadRegister(0x2000141f, sPortTemp);
                nLow = SerialSend.ReadRegister(0x2000141e, sPortTemp);
                limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                statetxt1.Text = limit.ToString("X2");

                nHigh = SerialSend.ReadRegister(0x20001421, sPortTemp);
                nLow = SerialSend.ReadRegister(0x20001420, sPortTemp);
                limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                statetxt2.Text = limit.ToString("X2");

                nHigh = SerialSend.ReadRegister(0x20001423, sPortTemp);
                nLow = SerialSend.ReadRegister(0x20001422, sPortTemp);
                limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                statetxt3.Text = limit.ToString("X2");

                Erangetxt.Text = SerialSend.ReadRegister(0x20001424, sPortTemp).ToString("X2");
                #endregion

                #region key_polling
                key_down_Delaytxt.Text = SerialSend.ReadRegister(0x2000140c, sPortTemp).ToString("X2");

                key_up_Delaytxt.Text = SerialSend.ReadRegister(0x2000140d, sPortTemp).ToString("X2");

                nHigh = SerialSend.ReadRegister(0x20001419, sPortTemp);
                nLow = SerialSend.ReadRegister(0x20001418, sPortTemp);
                limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                Aoc_key_Valuetxt.Text = limit.ToString("X2");

                #endregion
                adc_range_txt.Text = SerialSend.ReadRegister(0x20001192, sPortTemp).ToString("X2");
                Delay_Cat_Txt.Text = SerialSend.ReadRegister(0x2000122a, sPortTemp).ToString("X2");

                SystemTick0.Text = SerialSend.ReadRegister(0x80140800, sPortTemp).ToString("X2");
                SystemTick1.Text = SerialSend.ReadRegister(0x80140801, sPortTemp).ToString("X2");
                SystemTick2.Text = SerialSend.ReadRegister(0x80140802, sPortTemp).ToString("X2");
                SystemTick3.Text = SerialSend.ReadRegister(0x80140803, sPortTemp).ToString("X2");

                nHigh = SerialSend.ReadRegister(0x8050005b, sPortTemp);
                nLow = SerialSend.ReadRegister(0x8050005a, sPortTemp);
                limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
                adc_db_value_txt.Text = limit.ToString("X2");

                Wt_Pedestal_Day.Text = SerialSend.ReadRegister(0x8014089a, sPortTemp).ToString("X2");
                Wt_Pedestal_Night.Text = SerialSend.ReadRegister(0x8014089b, sPortTemp).ToString("X2");
                Wt_Pedestal_Midnight.Text = SerialSend.ReadRegister(0x8014089c, sPortTemp).ToString("X2");

                Max_Move_Px_Wt_Day.Text = SerialSend.ReadRegister(0x8014089d, sPortTemp).ToString("X2");
                Max_Move_Px_Wt_Night.Text = SerialSend.ReadRegister(0x8014089e, sPortTemp).ToString("X2");
                Max_Move_Px_Wt_Midnight.Text = SerialSend.ReadRegister(0x8014089f, sPortTemp).ToString("X2");
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                isWrite = false;
            }
           
           
        }

        #region combox选框


        private void CarBackLine_Pin_Sel_SelectedIndexChanged(object sender, EventArgs e)
        {
            var index = CarBackLine_Pin_Sel.SelectedIndex;
            if (index == -1)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"请查看补丁文件 \r\n 是否存在CarBackLine_Pin_Sel注释！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (CarBackLine_Pin_Sel.Text)
            {
                case "无":
                    binSetFlash(0x2000152b, 0x00, sPortTemp);
                    break;
                case "GPIO0":
                    binSetFlash(0x2000152b, 0x01, sPortTemp);
                    break;
                case "ICR_OFF":
                    binSetFlash(0x2000152b, 0x02, sPortTemp);
                    break;
                case "ICR_ON":
                    binSetFlash(0x2000152b, 0x03, sPortTemp);
                    break;
                case "TDN":
                    binSetFlash(0x2000152b, 0x04, sPortTemp);
                    break;
                default:
                    break;
            }
        }

        private void Mirror_Pin_Sel_SelectedIndexChanged(object sender, EventArgs e)
        {
            var index = Mirror_Pin_Sel.SelectedIndex;
            if (index == -1)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"请查看补丁文件 \r\n 是否存在Mirror_Pin_Sel注释！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (Mirror_Pin_Sel.Text)
            {
                case "无":
                    binSetFlash(0x2000152c, 0x00, sPortTemp);
                    break;
                case "GPIO0":
                    binSetFlash(0x2000152c, 0x01, sPortTemp);
                    break;
                case "ICR_OFF":
                    binSetFlash(0x2000152c, 0x02, sPortTemp);
                    break;
                case "ICR_ON":
                    binSetFlash(0x2000152c, 0x03, sPortTemp);
                    break;
                case "TDN":
                    binSetFlash(0x2000152c, 0x04, sPortTemp);
                    break;
                default:
                    break;
            }
        }

        private void Flip_Pin_Sel_SelectedIndexChanged(object sender, EventArgs e)
        {
            var index = Flip_Pin_Sel.SelectedIndex;
            if (index == -1)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"请查看补丁文件 \r\n 是否存在Flip_Pin_Sel注释！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (Flip_Pin_Sel.Text)
            {
                case "无":
                    binSetFlash(0x2000152d, 0x00, sPortTemp);
                    break;
                case "GPIO0":
                    binSetFlash(0x2000152d, 0x01, sPortTemp);
                    break;
                case "ICR_OFF":
                    binSetFlash(0x2000152d, 0x02, sPortTemp);
                    break;
                case "ICR_ON":
                    binSetFlash(0x2000152d, 0x03, sPortTemp);
                    break;
                case "TDN":
                    binSetFlash(0x2000152d, 0x04, sPortTemp);
                    break;
                default:
                    break;
            }
        }

        private void CarBackLine_Level_Toggle_SelectedIndexChanged(object sender, EventArgs e)
        {
            var index = CarBackLine_Level_Toggle.SelectedIndex;
            if (index == -1)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"请查看补丁文件 \r\n 是否存在CarBackLine_Level_Toggle注释！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (CarBackLine_Level_Toggle.Text)
            {
                case "高电平有":
                    binSetFlash(0x2000152a, 0x00, sPortTemp);
                    break;
                case "低电平有":
                    binSetFlash(0x2000152a, 0x01, sPortTemp);
                    break;
                default:
                    break;
            }
        }

        private void Mirror_Level_Toggle_SelectedIndexChanged(object sender, EventArgs e)
        {
            var index = Mirror_Level_Toggle.SelectedIndex;
            if (index == -1)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"请查看补丁文件 \r\n 是否存在Mirror_Level_Toggle注释！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (Mirror_Level_Toggle.Text)
            {
                case "高电平镜像":
                    binSetFlash(0x2000152e, 0x00, sPortTemp);
                    break;
                case "低电平镜像":
                    binSetFlash(0x2000152e, 0x01, sPortTemp);
                    break;
                default:
                    break;
            }
        }

        private void Flip_Level_Toggle_SelectedIndexChanged(object sender, EventArgs e)
        {
            var index = Flip_Level_Toggle.SelectedIndex;
            if (index == -1)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"请查看补丁文件 \r\n 是否存在Flip_Level_Toggle注释！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (Flip_Level_Toggle.Text)
            {
                case "高电平翻转":
                    binSetFlash(0x2000152f, 0x00, sPortTemp);
                    break;
                case "低电平翻转":
                    binSetFlash(0x2000152f, 0x01, sPortTemp);
                    break;
                default:
                    break;
            }
        }

        private void CarBackLine_Mode_SelectedIndexChanged(object sender, EventArgs e)
        {
            var index = CarBackLine_Mode.SelectedIndex;
            if (index == -1)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"请查看补丁文件 \r\n 是否存在CarBackLine_Mode注释！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (CarBackLine_Mode.Text)
            {
                case "每帧执行一次":
                    binSetFlash(0x20001529, 0x00, sPortTemp);
                    break;
                case "上电执行一次":
                    binSetFlash(0x20001529, 0x01, sPortTemp);
                    break;
                default:
                    break;
            }
        }

        private void Pwm_Pin_Sel_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isPwm_Pin_Sel) return;
            
            var index = Pwm_Pin_Sel.SelectedIndex;
            if (index == -1)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"请查看补丁文件 \r\n 是否存在Pin脚选择注释！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (Pwm_Pin_Sel.Text)
            {

                case "GPIO0":
                    binSetFlash(0x200015a4, 0x01, sPortTemp);//L
                    binSetFlash(0x200015a5, 0x00, sPortTemp);//m0
                    binSetFlash(0x200015a6, 0x00, sPortTemp);//m1
                    binSetFlash(0x200015a7, 0x00, sPortTemp);//h
                    break;
                case "GPIO1":
                    binSetFlash(0x200015a4, 0x02, sPortTemp);//L
                    binSetFlash(0x200015a5, 0x00, sPortTemp);//m0
                    binSetFlash(0x200015a6, 0x00, sPortTemp);//m1
                    binSetFlash(0x200015a7, 0x00, sPortTemp);//h
                    break;
                case "GPIO14":
                    binSetFlash(0x200015a4, 0x00, sPortTemp);//L
                    binSetFlash(0x200015a5, 0x40, sPortTemp);//m0
                    binSetFlash(0x200015a6, 0x00, sPortTemp);//m1
                    binSetFlash(0x200015a7, 0x00, sPortTemp);//h
                    break;
                case "GPIO15":
                    binSetFlash(0x200015a4, 0x00, sPortTemp);//L
                    binSetFlash(0x200015a5, 0x80, sPortTemp);//m0
                    binSetFlash(0x200015a6, 0x00, sPortTemp);//m1
                    binSetFlash(0x200015a7, 0x00, sPortTemp);//h
                    break;
                case "GPIO122":
                    binSetFlash(0x200015a4, 0x00, sPortTemp);//L
                    binSetFlash(0x200015a5, 0x00, sPortTemp);//m0
                    binSetFlash(0x200015a6, 0x40, sPortTemp);//m1
                    binSetFlash(0x200015a7, 0x00, sPortTemp);//h
                    break;
                case "GPIO123":
                    binSetFlash(0x200015a4, 0x00, sPortTemp);//L
                    binSetFlash(0x200015a5, 0x00, sPortTemp);//m0
                    binSetFlash(0x200015a6, 0x80, sPortTemp);//m1
                    binSetFlash(0x200015a7, 0x00, sPortTemp);//h
                    break;
                default:
                    break;
            }
        }

        private void TDN_In_C_SelectedIndexChanged(object sender, EventArgs e)
        {
            var index = TDN_In_C.SelectedIndex;
            if (index == -1)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"请查看补丁文件 \r\n 是否存在 TDN输入极性控制 注释！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (TDN_In_C.Text)
            {
                case "正向":
                    binSetFlash(0x2000122b, 0x00, sPortTemp);
                    break;
                case "反向":
                    binSetFlash(0x2000122b, 0x01, sPortTemp);
                    break;
                default:
                    break;
            }
        }

        private void IRC_In_C_SelectedIndexChanged(object sender, EventArgs e)
        {
            var index = IRC_In_C.SelectedIndex;
            if (index == -1)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"请查看补丁文件 \r\n 是否存在 IRC输出极性控制 注释！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (IRC_In_C.Text)
            {
                case "正向":
                    binSetFlash(0x20001233, 0x01, sPortTemp);
                    break;
                case "反向":
                    binSetFlash(0x20001233, 0x00, sPortTemp);
                    break;
                default:
                    break;
            }
        }

        #endregion

        #region  按键ADC值

        private void adc_up_txt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                adc_up_txt_Leave(sender, null);
            }
        }

        private void adc_up_txt_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(adc_up_txt.Text)) return;
            binSetFlash(0x20001189, (ulong)(Convert.ToInt32(adc_up_txt.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x20001188, (ulong)(Convert.ToInt32(adc_up_txt.Text, 16) & 0xff), sPortTemp);
        }

        private void adc_down_txt_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(adc_down_txt.Text)) return;
            binSetFlash(0x2000118b, (ulong)(Convert.ToInt32(adc_down_txt.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x2000118a, (ulong)(Convert.ToInt32(adc_down_txt.Text, 16) & 0xff), sPortTemp);
        }

        private void adc_down_txt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                adc_down_txt_Leave(sender, null);
            }
        }

        private void adc_right_txt_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(adc_right_txt.Text)) return;
            binSetFlash(0x2000118d, (ulong)(Convert.ToInt32(adc_right_txt.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x2000118c, (ulong)(Convert.ToInt32(adc_right_txt.Text, 16) & 0xff), sPortTemp);
        }

        private void adc_right_txt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                adc_right_txt_Leave(sender, null);
            }
        }

        private void adc_left_txt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                adc_left_txt_Leave(sender, null);
            }
        }

        private void adc_left_txt_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(adc_left_txt.Text)) return;
            binSetFlash(0x2000118f, (ulong)(Convert.ToInt32(adc_left_txt.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x2000118e, (ulong)(Convert.ToInt32(adc_left_txt.Text, 16) & 0xff), sPortTemp);
        }

        private void adc_menu_txt_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(adc_menu_txt.Text)) return;
            binSetFlash(0x20001191, (ulong)(Convert.ToInt32(adc_menu_txt.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x20001190, (ulong)(Convert.ToInt32(adc_menu_txt.Text, 16) & 0xff), sPortTemp);
        }
        
        private void adc_menu_txt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                adc_menu_txt_Leave(sender, null);
            }
        }

        private void adc_range_txt_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(adc_range_txt.Text)) return;
            binSetFlash(0x20001192, (ulong)(Convert.ToInt32(adc_range_txt.Text, 16) & 0xff), sPortTemp);
        }

        private void adc_range_txt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                adc_range_txt_Leave(sender, null);
            }
        }


        #endregion

        #region 文本填写框

        private void IR_All_GainLimit_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(IR_All_GainLimit.Text)) return;
            binSetFlash(0x2000122d, (ulong)(Convert.ToInt32(IR_All_GainLimit.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x2000122c, (ulong)(Convert.ToInt32(IR_All_GainLimit.Text, 16) & 0xff), sPortTemp);
        }

        private void IR_All_GainLimit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                IR_All_GainLimit_Leave(sender, null);
            }
        }

        private void IR_End_GainLimit_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(IR_End_GainLimit.Text)) return;
            binSetFlash(0x2000122f, (ulong)(Convert.ToInt32(IR_End_GainLimit.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x2000122e, (ulong)(Convert.ToInt32(IR_End_GainLimit.Text, 16) & 0xff), sPortTemp);
        }

        private void IR_End_GainLimit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                IR_End_GainLimit_Leave(sender, null);
            }
        }

        private void Delay_Cat_Txt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Delay_Cat_Txt_Leave(sender, null);
            }
        }

        private void Delay_Cat_Txt_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(Delay_Cat_Txt.Text)) return;
            binSetFlash(0x2000122a, (ulong)(Convert.ToInt32(Delay_Cat_Txt.Text, 16) & 0xff), sPortTemp);
        }

        private void Pulse_Out_GainLimit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Pulse_Out_GainLimit_Leave(sender, null);
            }
        }

        private void Pulse_Out_GainLimit_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(Pulse_Out_GainLimit.Text)) return;
            binSetFlash(0x20001483, (ulong)(Convert.ToInt32(Pulse_Out_GainLimit.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x20001482, (ulong)(Convert.ToInt32(Pulse_Out_GainLimit.Text, 16) & 0xff), sPortTemp);
        }

        private void Pulse_In_GainLimit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Pulse_In_GainLimit_Leave(sender, null);
            }
        }

        private void Pulse_In_GainLimit_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(Pulse_In_GainLimit.Text)) return;
            binSetFlash(0x20001485, (ulong)(Convert.ToInt32(Pulse_In_GainLimit.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x20001484, (ulong)(Convert.ToInt32(Pulse_In_GainLimit.Text, 16) & 0xff), sPortTemp);
        }
        private void IR_C_Model_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(IR_C_Model.Text)) return;
            binSetFlash(0x20001780, (ulong)(Convert.ToInt32(IR_C_Model.Text, 16) & 0xff), sPortTemp);
            binSetFlash(0x20001782, (ulong)(Convert.ToInt32(IR_C_Model.Text, 16) & 0xff), sPortTemp);
        }

        private void IR_C_Model_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                IR_C_Model_Leave(sender, null);
            }
        }

        #endregion

        #region 勾选框

        private void IR_C_Model_SelectedIndexChanged(object sender, EventArgs e)
        {
            var index = IR_C_Model.SelectedIndex;
            if (index == -1)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"请查看补丁文件 \r\n 是否存在IR_控制模式 注释！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (IR_C_Model.Text)
            {

                case "外部模式":
                    binSetFlash(0x20001780, 0x37, sPortTemp);
                    binSetFlash(0x20001782, 0x37, sPortTemp);
                    IR_All_GainLimit.Enabled = false;
                    IR_End_GainLimit.Enabled = false;
                    IR_All_AvgLimit.Enabled = false;
                    IR_End_AvgLimit.Enabled = false;
                    IR_End_AwbGainLimit.Enabled = false;
                    break;
                case "自动模式":
                    binSetFlash(0x20001780, 0x3b, sPortTemp);
                    binSetFlash(0x20001782, 0x3b, sPortTemp);
                    IR_All_GainLimit.Enabled = true;
                    IR_End_GainLimit.Enabled = true;
                    IR_All_AvgLimit.Enabled = true;
                    IR_End_AvgLimit.Enabled = true;
                    IR_End_AwbGainLimit.Enabled = true;
                    break;

                default:
                    break;
            }
        }
        private void OutPut_pin_SelectedIndexChanged(object sender, EventArgs e)
        {
            var index = OutPut_pin_Com.SelectedIndex;
            if (index == -1)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"请查看补丁文件 \r\n 是否存在OutPut_Pin脚选择 注释！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (OutPut_pin_Com.Text)
            {
                case "无":
                    binSetFlash(0x2000124f, 0x00, sPortTemp);
                    break;
                case "Setting_Sel":
                    binSetFlash(0x2000124f, 0x01, sPortTemp);
                    break;
                case "GPIO0":
                    binSetFlash(0x2000124f, 0x02, sPortTemp);
                    break;
                case "TDN":
                    binSetFlash(0x2000124f, 0x03, sPortTemp);
                    break;
                default:
                    break;
            }
        }

        private void Tve_Wave_sw_SelectedIndexChanged(object sender, EventArgs e)
        {
            var index = Tve_Wave_sw.SelectedIndex;
            if (index == -1)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"请查看补丁文件 \r\n 是否存在Tve_Wave_SW 注释！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (Tve_Wave_sw.Text)
            {
                case "打开":
                    binSetFlash(0x20001249, 0x01, sPortTemp);
                    break;
                case "关闭":
                    binSetFlash(0x20001249, 0x00, sPortTemp);
                    break;
                default:
                    break;
            }
        }
        bool isPwm_Pin_Sel = false;
        private void IRC_Enable_CheckedChanged(object sender, EventArgs e)
        {
            if (IRC_en.CheckState == CheckState.Checked)
            {
                binSetFlash(0x20001248, 0x01, sPortTemp);
                binSetFlash(0x80500189, 0x03, sPortTemp);
                //PWN_Ctr_Enable.Enabled = false;
                isPwm_Pin_Sel = true;
                Pwm_Pin_Sel.SelectedIndex = -1;
                Pwm_Pin_Sel.Properties.Items.Clear();
                Pwm_Pin_Sel.Properties.Items.Add("无");
                Pwm_Pin_Sel.Properties.Items.Add( "GPIO0");
                isPwm_Pin_Sel = false;
            }
            else
            {
                List<string> list = new List<string>() { "无", "GPIO0", "GPIO1", "GPIO14", "GPIO15" , "GPIO22" , "GPIO23" };
                binSetFlash(0x20001248, 0x00, sPortTemp);
                //PWN_Ctr_Enable.Enabled = true;
                isPwm_Pin_Sel = true;
                Pwm_Pin_Sel.SelectedIndex = -2;
                Pwm_Pin_Sel.Properties.Items.Clear();
                Pwm_Pin_Sel.Properties.Items.Add("无");
                Pwm_Pin_Sel.Properties.Items.Add("GPIO0");
                Pwm_Pin_Sel.Properties.Items.Add("GPIO1");
                Pwm_Pin_Sel.Properties.Items.Add("GPIO14");
                Pwm_Pin_Sel.Properties.Items.Add("GPIO15");
                Pwm_Pin_Sel.Properties.Items.Add("GPIO22");
                Pwm_Pin_Sel.Properties.Items.Add("GPIO23");
                isPwm_Pin_Sel = false;
            }
        }

        private void PWN_Ctr_Enable_CheckedChanged(object sender, EventArgs e)
        {
            if (PWN_Ctr_en.CheckState == CheckState.Checked)
            {
                binSetFlash(0x20001588, 0x01, sPortTemp);
                //IRC_Enable.Enabled = false;
            }
            else
            {
                binSetFlash(0x20001588, 0x00, sPortTemp);
                //IRC_Enable.Enabled = true;
            }
        }

        private void Setting_Polling_ENable_CheckedChanged(object sender, EventArgs e)
        {
            if (Setting_Polling_ENable.CheckState == CheckState.Checked)
            {
                binSetFlash(0x80140b1e, 0x01, sPortTemp);
            }
            else
            {
                binSetFlash(0x80140b1e, 0x00, sPortTemp);
            }
        }

        private void System_Tick_Enable_CheckedChanged(object sender, EventArgs e)
        {
            if (System_Tick_Enable.CheckState == CheckState.Checked)
            {
                binSetFlash(0x801407fc, 0x01, sPortTemp);
            }
            else
            {
                binSetFlash(0x801407fc, 0x00, sPortTemp);
            }
        }
        private void adc_db_enable_CheckedChanged(object sender, EventArgs e)
        {
            var temp = SerialSend.ReadRegister(0x80500253, sPortTemp);
            if (adc_db_en.CheckState == CheckState.Checked)
            {
                temp = temp | 0x80;
                binSetFlash(0x80500253, (ulong)temp, sPortTemp);
            }
            else {
                temp = temp & 0x7f;
                binSetFlash(0x80500253, (ulong)temp, sPortTemp);
            }
        }
        #endregion

        #region setting 轮询

        private void SystemTick0_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(SystemTick0.Text)) return;
            binSetFlash(0x80140800, (ulong)(Convert.ToInt32(SystemTick0.Text, 16) & 0xff), sPortTemp);
        }

        private void SystemTick0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SystemTick0_Leave(sender, null);
            }
        }

        private void SystemTick1_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(SystemTick1.Text)) return;
            binSetFlash(0x80140801, (ulong)(Convert.ToInt32(SystemTick1.Text, 16) & 0xff), sPortTemp);
        }

        private void SystemTick1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SystemTick1_Leave(sender, null);
            }
        }

        private void SystemTick2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(SystemTick2.Text)) return;
            binSetFlash(0x80140802, (ulong)(Convert.ToInt32(SystemTick2.Text, 16) & 0xff), sPortTemp);
        }

        private void SystemTick2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SystemTick2_Leave(sender, null);
            }

        }

        private void SystemTick3_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(SystemTick3.Text)) return;
            binSetFlash(0x80140803, (ulong)(Convert.ToInt32(SystemTick3.Text, 16) & 0xff), sPortTemp);
        }

        private void SystemTick3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SystemTick3_Leave(sender, null);
            }

        }



        #endregion

        private void btn_GetMode_Click(object sender, EventArgs e)
        {
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            var  nHigh = SerialSend.ReadRegister(0x8050025b, sPortTemp);
            var  nLow = SerialSend.ReadRegister(0x8050025a, sPortTemp);
            var  limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
            adc_db_value_txt.Text = limit.ToString("X2");
        }
        #region  Patch_3DNR

        private void Wt_Pedestal_Day_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Wt_Pedestal_Day_Leave(sender, null);
            }
        }

        private void Wt_Pedestal_Night_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Wt_Pedestal_Night_Leave(sender, null);
            }
        }

        private void Wt_Pedestal_Midnight_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Wt_Pedestal_Midnight_Leave(sender, null);
            }
        }

        private void Max_Move_Px_Wt_Day_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Max_Move_Px_Wt_Day_Leave(sender, null);
            }
        }

        private void Max_Move_Px_Wt_Night_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Max_Move_Px_Wt_Night_Leave(sender, null);
            }
        }

        private void Max_Move_Px_Wt_Midnight_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Max_Move_Px_Wt_Midnight_Leave(sender, null);
            }
        }

        private void Wt_Pedestal_Day_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(Wt_Pedestal_Day.Text)) return;
            binSetFlash(0x8014089a, (ulong)(Convert.ToInt32(Wt_Pedestal_Day.Text, 16) & 0xff), sPortTemp);
        }

        private void Wt_Pedestal_Night_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(Wt_Pedestal_Night.Text)) return;
            binSetFlash(0x8014089b, (ulong)(Convert.ToInt32(Wt_Pedestal_Night.Text, 16) & 0xff), sPortTemp);
        }

        private void Wt_Pedestal_Midnight_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(Wt_Pedestal_Midnight.Text)) return;
            binSetFlash(0x8014089c, (ulong)(Convert.ToInt32(Wt_Pedestal_Midnight.Text, 16) & 0xff), sPortTemp);
        }

        private void Max_Move_Px_Wt_Day_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(Max_Move_Px_Wt_Day.Text)) return;
            binSetFlash(0x8014089d, (ulong)(Convert.ToInt32(Max_Move_Px_Wt_Day.Text, 16) & 0xff), sPortTemp);
        }

        private void Max_Move_Px_Wt_Night_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(Max_Move_Px_Wt_Night.Text)) return;
            binSetFlash(0x8014089e, (ulong)(Convert.ToInt32(Max_Move_Px_Wt_Night.Text, 16) & 0xff), sPortTemp);
        }

        private void Max_Move_Px_Wt_Midnight_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(Max_Move_Px_Wt_Midnight.Text)) return;
            binSetFlash(0x8014089f, (ulong)(Convert.ToInt32(Max_Move_Px_Wt_Midnight.Text, 16) & 0xff), sPortTemp);
        }
        #endregion

        private void IR_All_AvgLimit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                IR_All_AvgLimit_Leave(sender, null);
            }

        }

        private void IR_End_AvgLimit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                IR_End_AvgLimit_Leave(sender, null);
            }

        }

        private void IR_End_AwbGainLimit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                IR_End_AwbGainLimit_Leave(sender, null);
            }
        }

        private void IR_All_AvgLimit_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(IR_All_AvgLimit.Text)) return;
            binSetFlash(0x20001235, (ulong)(Convert.ToInt32(IR_All_AvgLimit.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x20001234, (ulong)(Convert.ToInt32(IR_All_AvgLimit.Text, 16) & 0xff), sPortTemp);
        }

        private void IR_End_AvgLimit_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(IR_End_AvgLimit.Text)) return;
            binSetFlash(0x20001237, (ulong)(Convert.ToInt32(IR_End_AvgLimit.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x20001236, (ulong)(Convert.ToInt32(IR_End_AvgLimit.Text, 16) & 0xff), sPortTemp);
        }

        private void IR_End_AwbGainLimit_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(IR_End_AwbGainLimit.Text)) return;
            binSetFlash(0x2000123b, (ulong)(Convert.ToInt32(IR_End_AwbGainLimit.Text, 16) >> 12 & 0xff), sPortTemp);
            binSetFlash(0x2000123a, (ulong)(Convert.ToInt32(IR_End_AwbGainLimit.Text, 16) >> 10 & 0xff), sPortTemp);
            binSetFlash(0x20001239, (ulong)(Convert.ToInt32(IR_End_AwbGainLimit.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x20001238, (ulong)(Convert.ToInt32(IR_End_AwbGainLimit.Text, 16) & 0xff), sPortTemp);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            //var nHigh = SerialSend.ReadRegister(0x8014010a, sPortTemp);
            //var nLow = SerialSend.ReadRegister(0x8014010b, sPortTemp);
            //var limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
            //Cur_Gain.Text = limit.ToString("X2");

            var nHigh = SerialSend.ReadRegister(0x20000153, sPortTemp);
            var m1 = SerialSend.ReadRegister(0x20000152, sPortTemp);
            var m0 = SerialSend.ReadRegister(0x20000151, sPortTemp);
            var nLow = SerialSend.ReadRegister(0x20000150, sPortTemp);

            Cur_Gain.Text = Convert.ToInt32((nHigh.ToString("X2") + m1.ToString("X2") + m0.ToString("X2") + nLow.ToString("X2")),16).ToString("X2");


            nHigh = SerialSend.ReadRegister(0x20000147, sPortTemp);
            nLow = SerialSend.ReadRegister(0x20000146, sPortTemp);
            var limit = ((nHigh << 8 & 0xff00) | (nLow & 0xff));
            Cur_Avg.Text = limit.ToString("X2");

            nHigh = SerialSend.ReadRegister(0x80305514, sPortTemp);
            m1 = SerialSend.ReadRegister(0x80305515, sPortTemp);
            m0 = SerialSend.ReadRegister(0x80305516, sPortTemp);
            nLow = SerialSend.ReadRegister(0x80305517, sPortTemp);
            string hexString1= nHigh.ToString("X2") + m1.ToString("X2") + m0.ToString("X2") + nLow.ToString("X2"); 
            nHigh = SerialSend.ReadRegister(0x80305510, sPortTemp);
            m1 = SerialSend.ReadRegister(0x80305511, sPortTemp);
            m0 = SerialSend.ReadRegister(0x80305512, sPortTemp);
            nLow = SerialSend.ReadRegister(0x80305513, sPortTemp);
            string hexString2 = nHigh.ToString("X2") + m1.ToString("X2") + m0.ToString("X2") + nLow.ToString("X2");
            limit = Convert.ToInt32(hexString1, 16) * 128 / Convert.ToInt32(hexString2, 16);
            Cur_IRGain.Text = limit.ToString("X2");
        }

        private void simpleButton5_MouseEnter(object sender, EventArgs e)
        {
            isWrite = true;
        }

        private void simpleButton5_MouseLeave(object sender, EventArgs e)
        {
            isWrite = false;
        }

        private void adc_chg_mask_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(adc_up_txt.Text)) return;
            binSetFlash(0x80500257, (ulong)(Convert.ToInt32(adc_chg_mask.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x80500256, (ulong)(Convert.ToInt32(adc_chg_mask.Text, 16) & 0xff), sPortTemp);
        }

        private void adc_chg_mask_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                adc_chg_mask_Leave(sender, null);
            }
        }

        private void statetxt0_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(statetxt0.Text)) return;
            binSetFlash(0x2000141d, (ulong)(Convert.ToInt32(statetxt0.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x2000141c, (ulong)(Convert.ToInt32(statetxt0.Text, 16) & 0xff), sPortTemp);
        }
        
        private void statetxt1_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(statetxt1.Text)) return;
            binSetFlash(0x2000141f, (ulong)(Convert.ToInt32(statetxt1.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x2000141e, (ulong)(Convert.ToInt32(statetxt1.Text, 16) & 0xff), sPortTemp);
        }

        private void statetxt2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(statetxt2.Text)) return;
            binSetFlash(0x20001421, (ulong)(Convert.ToInt32(statetxt2.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x20001420, (ulong)(Convert.ToInt32(statetxt2.Text, 16) & 0xff), sPortTemp);
        }

        private void statetxt3_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(statetxt3.Text)) return;
            binSetFlash(0x20001423, (ulong)(Convert.ToInt32(statetxt3.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x20001422, (ulong)(Convert.ToInt32(statetxt3.Text, 16) & 0xff), sPortTemp);
        }

        private void Erangetxt_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(Erangetxt.Text)) return;
            binSetFlash(0x20001424, (ulong)(Convert.ToInt32(Erangetxt.Text, 16) & 0xff), sPortTemp);
        }

        private void statetxt0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                statetxt0_Leave(sender, null);
            }
        }

        private void statetxt1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                statetxt1_Leave(sender, null);
            }
        }

        private void statetxt2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                statetxt2_Leave(sender, null);
            }
        }

        private void statetxt3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                statetxt3_Leave(sender, null);
            }
        }

        private void Erangetxt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Erangetxt_Leave(sender, null);
            }
        }

        private void key_down_Delaytxt_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(key_down_Delaytxt.Text)) return;
            binSetFlash(0x2000140c, (ulong)(Convert.ToInt32(key_down_Delaytxt.Text, 16) & 0xff), sPortTemp);
            
        }

        private void key_up_Delaytxt_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(key_up_Delaytxt.Text)) return;
            binSetFlash(0x2000140d, (ulong)(Convert.ToInt32(key_up_Delaytxt.Text, 16)  & 0xff), sPortTemp);
            
        }

        private void Aoc_key_Valuetxt_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (string.IsNullOrEmpty(Aoc_key_Valuetxt.Text)) return;
            binSetFlash(0x20001419, (ulong)(Convert.ToInt32(Aoc_key_Valuetxt.Text, 16) >> 8 & 0xff), sPortTemp);
            binSetFlash(0x20001418, (ulong)(Convert.ToInt32(Aoc_key_Valuetxt.Text, 16) & 0xff), sPortTemp);
        }

        private void key_down_Delaytxt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                key_down_Delaytxt_Leave(sender, null);
            }
        }

        private void key_up_Delaytxt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                key_up_Delaytxt_Leave(sender, null);
            }
        }

        private void Aoc_key_Valuetxt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Aoc_key_Valuetxt_Leave(sender, null);
            }

        }

        private void binSetFlash(ulong addr , ulong val, SerialPort sPort)
        {
            if (IsUsbOpen)
            {

                if (null != BinHelp.usbList)
                {
                    uint pVule = 0x0;
                    var isbool = false;
                    if (IntPtr.Size == 8)
                    {
                        isbool = XChipUSB.UsbRegisterRead64(BinHelp.CurrentSize, ref pVule, true);
                        SerialSend.Sendser("R:0x" + (BinHelp.CurrentSize).ToString("X2"), pVule.ToString("X2"));
                    }
                    else
                    {
                        isbool = XChipUSB.UsbRegisterRead86(BinHelp.CurrentSize, ref pVule, true);
                        SerialSend.Sendser("R:0x" + (BinHelp.CurrentSize).ToString("X2"), pVule.ToString("X2"));
                    }
                    var chang = (int)(pVule >> 16); // 使用右移16位操作来获取高16位  
                    var kuang = (int)(pVule & 0xFFFF); // 使用位与操作来获取低16位  

                    BinHelp.SstBinUsbUpdateData(ref BinHelp.BinList, addr.ToString("X2"), val.ToString("X"), chang, kuang);
                }
            }
            else if (m_open)
            {

            }
            else
            {
                if (!sPortTemp.IsOpen)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                    return;
                }

                if (null != BinHelp.lspList)
                {   //编写Bin更新数据
                    var temp = SerialSend.ReadRegister(BinHelp.BootSettingIdx, sPortTemp);
                    BinHelp.SetBinUpdateData(ref BinHelp.BinList, addr.ToString("X2"), val.ToString("X"), (uint)temp,true);
                }
            }
            SerialSend.WriteRegister(addr, val, sPortTemp);
        }
    }
}
